/*
 *  Copyright (c) 2022 ZhuHai Jieli Technology Co.,Ltd.
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

#include "typedef.h"

typedef struct U16BIT {
    u8 b0 : 1;
    u8 b1 : 1;
    u8 b2 : 1;
    u8 b3 : 1;
    u8 b4 : 1;
    u8 b5 : 1;
    u8 b6 : 1;
    u8 b7 : 1;
    u8 b8 : 1;
    u8 b9 : 1;
    u8 b10 : 1;
    u8 b11 : 1;
    u8 b12 : 1;
    u8 b13 : 1;
    u8 b14 : 1;
    u8 b15 : 1;
} U16BIT ;

typedef union UBIT16 {
    U16BIT bits;
    u16 val;
} UBIT16;

static UBIT16 cd1;
static u16 do16(void)
{
    UBIT16 ufcc, urp;
    u8 f_xdat ;
    u8 f_datin;
    ufcc.bits = urp.bits = cd1.bits;
    f_datin  = 0x10 ;
    f_xdat = f_datin ^ urp.bits.b15;

    ufcc.val = urp.val << 1;
    ufcc.bits.b12 =  urp.bits.b11 ^ f_xdat;
    ufcc.bits.b5 = urp.bits.b4 ^ f_xdat ;
    ufcc.bits.b0 = f_xdat ;

    urp.bits = ufcc.bits;
    cd1.bits = ufcc.bits ;
    return ufcc.val & 0x00ff;
}

void doe(u16 k, void *pBuf, u32 lenIn, u32 addr)
{
    if (addr) {
        k = (addr >> 2) ^ k;
    }
    u8 *p = (u8 *)pBuf;
    cd1.val = k;
    *p++ ^= k;
    while (--lenIn) {
        *p++ ^= do16();
    }
}

